//
//  TravetTourDetailsViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/18.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravetTourDetailsViewController.h"
#import "TravelTourBigModel.h"
#import "TravelTourOneTableCell.h"
#import "TravelTourTwoTableViewCell.h"
#import "TravelTourThreeTableViewCell.h"
#import "DataAnalysis.h"
#import "TravelTopicDetailsOneTableViewCell.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>
#import "TravelTourBtnTipsViewController.h"
#import "TravelTourImagePicViewController.h"
#import "MapViewController.h"

@interface TravetTourDetailsViewController ()<UITableViewDataSource, UITableViewDelegate,TravelTourThreeViewCell>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableDictionary *bigDic;

@property (nonatomic, retain) NSMutableArray *arrBig;

@property (nonatomic, retain) UIView *titleView;

@property (nonatomic, retain) UIImageView *imageP;

@property (nonatomic, retain) UILabel *labelName_en;

@property (nonatomic, retain) UILabel *labelName_zh_cn;

@property (nonatomic, retain) UIButton *btnWeb;

@property (nonatomic, retain) UIButton *btnMap;


@end

@implementation TravetTourDetailsViewController

-(void)dealloc
{
    [_tableView release];
    [_bigDic release];
    [_arrBig release];
    [_titleView release];
    [_imageP release];
    [_labelName_en release];
    [_labelName_zh_cn release];
    [_btnWeb release];
    [_btnMap release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arrBig = [NSMutableArray array];
    
    [self loading];
    [self createTableView];
 
    [self createTitle];   
    
}

#pragma mark创建tableView
-(void)createTableView
{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44 *kHeight, self.view.frame.size.width, self.view.frame.size.height - 44 * kHeight) style:UITableViewStyleGrouped];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[TravelTourOneTableCell class] forCellReuseIdentifier:@"TravelTourOneTableCell"];
    self.tableView.tableFooterView = [[[UIView alloc]initWithFrame:CGRectZero] autorelease];
    [self.tableView registerClass:[TravelTourThreeTableViewCell class] forCellReuseIdentifier:@"TravelTourThreeTableViewCell"];
    self.tableView.showsVerticalScrollIndicator = NO;

    [self.view addSubview:self.tableView];
    [_tableView release];
 
}

#pragma mark 网络请求
-(void)loading
{
    [DataAnalysis dataAnalysisGETWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json?page=1",self.idNumber] parameters:nil showHuD:YES hiddenView:nil successfulBlock:^(id responseObject) {
        
       
        self.bigDic = responseObject;
        
        
        if ([[self.bigDic valueForKey:@"attraction_trip_tags"] count] == 0) {
            
            UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64 *kHeight, self.view.frame.size.width, self.view.frame.size.height - 64 * kHeight)];
            
            
            [self.view addSubview:view];
            
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 *kHeight)];
            [imageV sd_setImageWithURL:[NSURL URLWithString:[self.bigDic valueForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"Travel_place"]];
       
            imageV.userInteractionEnabled = YES;
            
            [view addSubview:imageV];
            
            
           UILabel *labelName_zh = [[UILabel alloc]initWithFrame:CGRectMake(10 *kWidth, imageV.frame.size.height / 4 *3, imageV.frame.size.width / 2 - 10*kWidth, 25 *kHeight)];
            
            
            
            labelName_zh.font = [UIFont systemFontOfSize:17 *kWidth];
           labelName_zh.textColor = [UIColor whiteColor];
           labelName_zh.text = [self.bigDic valueForKey:@"name_zh_cn"];
            [imageV addSubview:labelName_zh];
            [labelName_zh release];
            
    
            
            UIButton *btnMp = [UIButton buttonWithType:UIButtonTypeCustom];
           btnMp.frame = CGRectMake(imageV.frame.size.width / 4 * 2 + 40 * kWidth, labelName_zh.frame.origin.y , labelName_zh.frame.size.width, 20 *kHeight);
            [btnMp setTitle:@"地图" forState:UIControlStateNormal];
            [btnMp addTarget:self action:@selector(btnMapAction) forControlEvents:UIControlEventTouchUpInside];
            [imageV addSubview:btnMp];
            
            
            UILabel *labelSay = [[ UILabel alloc ]initWithFrame:CGRectMake(40 * kWidth, imageV.frame.size.height + imageV.frame.origin.y + 100 * kHeight, self.view.frame.size.width - 80 * kWidth, 60 *kHeight)];
            labelSay.textAlignment = NSTextAlignmentCenter;
            labelSay.numberOfLines = 0;
            labelSay.lineBreakMode = NSLineBreakByWordWrapping;
            labelSay.text = @"亲!这个地方好像没有宝宝们去过呐!待你前往哦!";
            
            [view addSubview:labelSay];
            [labelSay release];
            
        }else{
        
        
        NSArray *arr = [self.bigDic valueForKey:@"attraction_trip_tags"];
        
        for (NSDictionary *dic in arr) {
            
            TravelTourBigModel *model = [TravelTourBigModel travelTourBigModelWithNSDictionary:dic];
            
            [self.arrBig addObject:model];
        }
        }
        
        
               [self.tableView reloadData];
      
    } failureBlock:^(NSError *error) {
        NSLog(@"%@", error);
        
    }];
  
}







#pragma mark 创建标题
-(void)createTitle
{
    self.view.backgroundColor = [UIColor purpleColor];
    
    self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,kHeight * 64)];
    self.titleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.titleView];
    [self.titleView release];
    [_titleView release];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(15 * kWidth, 25 * kHeight, 20 * kWidth, 20 * kHeight);
    [backBtn setBackgroundImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    [self.titleView addSubview:backBtn];
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 130 * kWidth, self.titleView.frame.size.height / 2 - 10 * kHeight, kWidth * 260, kHeight * 30)];
    
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text =  @"旅行地详情";
    titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [self.titleView addSubview:titleLabel];
    [titleLabel release];
}

//返回点击事件
-(void)backAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        
        return [[self.arrBig[section - 1] arrTourBig] count];
    }
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.arrBig.count ==0) {
        
        return 0;
    }else{
        return self.arrBig.count +1;
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if ([[self.bigDic valueForKey:@"description"] isKindOfClass:[NSNull class]]) {
            return 30;
        }else{
        
        CGFloat frameOne = [TravelTopicDetailsOneTableViewCell heightOfLabel:[self.bigDic valueForKey:@"description"] andTextFont:[UIFont systemFontOfSize:14*kWidth] andTextWidth:self.view.frame.size.width -20 *kWidth];
        return frameOne +20*kHeight;
        }
    }else{
        
        if (self.arrBig.count ==0) {
            return 0;
        }else{
        CGFloat frame  = [TravelTopicDetailsOneTableViewCell heightOfLabel:[[self.arrBig[indexPath.section - 1] arrTourBig][indexPath.row] DES] andTextFont:[UIFont systemFontOfSize:14 *kWidth] andTextWidth:self.view.frame.size.width - 10 *kWidth];
  
       
        
        if ([[[self.arrBig[indexPath.section - 1] arrTourBig] [indexPath.row] arrNotes] count]  == 0)
        {
            
            return frame + 30*kHeight;
            
        }else
        {
            return frame +190 *kHeight;
        }
        }
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
                  TravelTourOneTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelTourOneTableCell" forIndexPath:indexPath];
            
            cell.selectionStyle =UITableViewCellSelectionStyleNone;
            
            cell.str = [self.bigDic valueForKey:@"description"];
            
            return cell;
            }else{
        
        TravelTourThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelTourThreeTableViewCell" forIndexPath:indexPath];
                cell.selectionStyle =UITableViewCellSelectionStyleNone;
               
                    cell.model = [self.arrBig[indexPath.section - 1] arrTourBig][indexPath.row];
               
                
                    cell.delegate = self;
                    return cell;

    }
 
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        self.imageP = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 *kHeight)];
        [self.imageP sd_setImageWithURL:[NSURL URLWithString:[self.bigDic valueForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"Travel_place"]];
  
        
        self.imageP.userInteractionEnabled = YES;

       

        
        if ([[self.bigDic valueForKey:@"name_zh_cn"] isKindOfClass:[NSNull class]]) {

            
        }else{
        self.labelName_zh_cn = [[UILabel alloc]initWithFrame:CGRectMake(10 *kWidth, self.imageP.frame.size.height / 4 *3, self.imageP.frame.size.width / 2 - 10*kWidth, 25 *kHeight)];
        self.labelName_zh_cn.font = [UIFont systemFontOfSize:17 *kWidth];
        self.labelName_zh_cn.textColor = [UIColor whiteColor];
          self.labelName_zh_cn.text = [self.bigDic valueForKey:@"name_zh_cn"];
        [self.imageP addSubview:self.labelName_zh_cn];
        [_labelName_zh_cn release];
        }
        if ([[self.bigDic valueForKey:@"name_en"] isKindOfClass:[NSNull class]]) {
            
        }else{

        self.labelName_en = [[UILabel alloc]initWithFrame:CGRectMake(10 *kHeight, self.labelName_zh_cn.frame.size.height +self.labelName_zh_cn.frame.origin.y, self.labelName_zh_cn.frame.size.width, self.labelName_zh_cn.frame.size.height)];
        self.labelName_en.font = [UIFont systemFontOfSize:14 *kWidth];
        self.labelName_en.textColor = [UIColor whiteColor];
      
            self.labelName_en.text = [self.bigDic valueForKey:@"name_en"];
[self.imageP addSubview:self.labelName_en];
            [_labelName_en release];

        }
        
      
        

        
        

        self.btnWeb = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btnWeb.frame = CGRectMake(self.imageP.frame.size.width / 2, self.labelName_en.frame.origin.y, self.imageP.frame.size.width / 4, 20 *kHeight);
        [self.btnWeb setTitle:@"实用贴士" forState:UIControlStateNormal];
        [self.btnWeb addTarget:self action:@selector(btnWebAction) forControlEvents:UIControlEventTouchUpInside];
        [self.imageP addSubview:self.btnWeb];
        
        self.btnMap = [UIButton buttonWithType:UIButtonTypeCustom];
        self.btnMap.frame = CGRectMake(self.imageP.frame.size.width / 4 * 3, self.btnWeb.frame.origin.y, self.btnWeb.frame.size.width, 20 *kHeight);
        [self.btnMap setTitle:@"地图" forState:UIControlStateNormal];
        [self.btnMap addTarget:self action:@selector(btnMapAction) forControlEvents:UIControlEventTouchUpInside];
        [self.imageP addSubview:self.btnMap];
        return self.imageP;

    }else{
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40*kHeight)];
   label.backgroundColor = [UIColor colorWithRed:0.953 green:0.952 blue:0.973 alpha:1.000];
    label.text = [self.arrBig[section - 1] name];
    label.font = [UIFont systemFontOfSize:17 *kWidth];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor blackColor];
    return label;
    }
}
#pragma mark 实用贴士点击跳转
-(void)btnWebAction
{
    TravelTourBtnTipsViewController *tips = [[TravelTourBtnTipsViewController alloc]init];
    tips.strTips = [self.bigDic valueForKey:@"tips_html"];
     tips.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:tips animated:YES completion:nil];
    
}
#pragma mark 地图点击跳转
-(void)btnMapAction
{
    MapViewController *map = [[MapViewController alloc]init];
    
       
    map.Wlatitude = [[self.bigDic valueForKey:@"lat"] floatValue];
    map.Wlongitude = [[self.bigDic valueForKey:@"lng"] floatValue];
    
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:map animated:YES];
  
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 200 *kHeight;
    }else{
        
        return 40*kHeight;
    }
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    
    return 1;
}
#pragma mark cell的代理方法
-(void)pushToImageOivVC:(NSMutableArray *)arr Number:(NSInteger)number
{
    
    TravelTourImagePicViewController *view = [[TravelTourImagePicViewController alloc]init];
    view.arr = arr;
    view.number = number;
    [self.navigationController pushViewController:view animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
